package cn.rokhdelar.ams.common.domain.vo;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.util.Collections;
import java.util.List;

/**
 * 分页查询结果VO
 * @param <T> 结果类型
 */
@Data
@Schema(description = "分页查询结果实体")
public class PageVO<T> {
    @Schema(description = "总条数")
    private Long total;
    @Schema(description = "总页数")
    private Long pages;
    @Schema(description = "数据集合")
    private List<T> list;

    public static <PO, VO> PageVO<VO> of(Page<PO> page, Class<VO> voClass) {
        PageVO<VO> vo = new PageVO<>();
        vo.setTotal(page.getTotal());
        vo.setPages(page.getPages());
        List<PO> records = page.getRecords();
        if (CollUtil.isEmpty(records)) {
            vo.setList(Collections.emptyList());
            return vo;
        }
        vo.setList(BeanUtil.copyToList(records, voClass));
        return vo;
    }

    public static  <VO>PageVO<VO> fromList(int size,Long count,List<VO> records ) {
        PageVO<VO> vo = new PageVO<>();
        vo.setTotal(count);
        vo.setPages((long) Math.ceil((double) count / size));
        vo.setList(records);
        return vo;
    }
}
